home *** CD-ROM | disk | FTP | other *** search
- Path: atglab.bls.com!Alun.Champion
- From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
- Newsgroups: comp.lang.c++
- Subject: Re: destructots and VC++
- Date: 19 Jan 1996 19:14:00 GMT
- Organization: Computer People Inc.
- Message-ID: <ALUN.CHAMPION.96Jan19141400@g7240065.bridge.bst.bls.com>
- References: <4doem9$7a8@zephyr.ens.tek.com>
- NNTP-Posting-Host: bstfirewall.bst.bls.com
- In-reply-to: Adam Smith's message of 19 Jan 1996 15:51:37 GMT
-
- In article <4doem9$7a8@zephyr.ens.tek.com> Adam Smith <Adam_Smith@Lightworks-london.ccmail.compuserve.com> writes:
-
- : Has anyone noticed that the following does not work in VC++ 2.0.
-
- : Kipper* smoked = new Kipper();
- : smoked->~Kipper();
-
- : This is just a noddy example, and is legal.
-
- How do you mean "does not work" ?
- It doesn't compile.
- It doesn't have expected results.
-
- Some compilers still require an explicit declaration of a destructor for
- this to compile.
- The same as some compilers require an explicit declaration of operator= to
- be able to call
-
- a.operator=(b);
-
- Which is a pain in the arse when trying to call a base class operator = when
- it doesn't have on defined
-
- class A
- { };
-
- class B : public A
- {
- public:
- const B& operator = (const B& b)
- {
- if (this == &b)
- return *this;
-
- // you have to use
- *(A*)this = *(A*)&b;
- // instead of
- // A::operator=(b);
- return *this;
- }
- };
-
- Slowly but surely they are sorting these problems out.
-
- Explicit calls to destructors can have all sorts of pit falls:
-
- calling a virtual destructor, when not meaning to.
- calling a destructor on a stack object.
- etc..
-
- There is very little need to call destructors explicitly, one such case
- is in the operator = when defining the operator = in terms of the copy
- constructor and using new placement syntax
-
- Example:
-
- #include <new.h>
-
- class A
- {
- public:
- A(const A&); // Complicated copy constructor.
-
- const A& operator = (const A& a)
- {
- if (this != &a) {
- this->A::~A(); // Must ensure calling destructor for this class.
- new (this) A(a);
- }
- return *this;
- }
- ...
- };
-
- Regards
-
- -A.
- --
- | A.Champion |
-